<?php
/**
 *	Logic of Servers.
 *	@category		cmTools
 *	@package		BugTracker.Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			0.1
 *	@version		$Id$
 */
/**
 *	Logic of Servers.
 *	@category		cmTools
 *	@package		BugTracker.Logic
 *	@extends		CMF_Krypton_Core_Logic
 *	@uses			Model_Server
 *	@uses			Logic_Project
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			0.1
 *	@version		$Id$
 */
class Logic_Server extends CMF_Krypton_Core_Logic
{
	public static function addServer( $data, $prefix = "" )
	{
		$data	= self::removePrefixFromFields( $data, $prefix );
		self::validateForm( 'administration/server', 'addServer', $data, $prefix );
		$data['created']	= time();
		$model	= new Model_Server();
		return $model->insert( $data );
	}
	
	public static function editServer( $serverId, $data, $prefix = "" )
	{
		if( !$serverId )
		 	throw new CMF_Krypton_Exception_Logic( 'server_id_missing' );
		$model	= new Model_Server( $serverId );
		if( !$model->exists() )
			throw new CMF_Krypton_Exception_Logic( 'invalid_server_id' );
		$data	= self::removePrefixFromFields( $data, $prefix );
		self::validateForm( 'administration/server', 'editServer', $data, $prefix );
		$result	= $model->update( $data );
		if( $result )
			$model->update( array( 'modified' => time() ) );
		return $result;
	}
	
	public static function getServerData( $serverId )
	{
		$model	= new Model_Server( $serverId );
		$data	= $model->get();
		if( !$data )
			throw new CMF_Krypton_Exception_Logic( 'invalid_server_id' );
		return $data;
	}
	
	public static function getServerDescription( $serverId )
	{
		$data	= self::getServerData( $serverId );
		return $data['description'];
	}
	
	public static function getServerList( $assoc = FALSE )
	{
		$projects	= array();
		$model	= new Model_Server();
		$list	= $model->find();
		return $list;
	}
	
	public static function getServers( $assoc = FALSE )
	{
		$servers	= array();
		$model	= new Model_Server();
		$list	= $model->find();
		foreach( $list as $entry )
			$servers[$entry['serverId']]	= $entry['title'];
		return $servers;
	}
	
	public static function getServerTitle( $serverId )
	{
		$data	= self::getServerData( $serverId );
		return $data['title'];
	}

	public static function removeServer( $serverId )
	{
		$model	= new Model_Server( $serverId );
		if( !$model->exists() )
			throw new LogicException( 'invalid_server_id' );
/*	@todo	CONSTRAINT !
		if( class_exists( Logic_Project) )
		{
			$projects	= Logic_Project::getProjectsFromServer( $serverId );
			foreach( $projects as $projectId )
				Logic_Project::removeProjectFromServer( $projectId, $serverId );
		}
*/		$model->delete();
	}
}
?>
